#!/usr/bin/python3.5
# -*- coding: utf-8 -*-  

from libs.public import *
import random
import time
import io
import hashlib

class AutoReply:
    kefu = 'kefu.jpg'   #客服页
    screen = 'screen.jpg'  #截取的图片
    shop = 'shop.jpg'  #店铺名称
    send_file = 'send_list.txt' #记录发送记录
    def image_cut_save(self, path, save_path):
        """
            所截区域图片保存
        :param path: 图片路径
        :param left: 区块左上角位置的像素点离图片左边界的距离
        :param upper：区块左上角位置的像素点离图片上边界的距离
        :param right：区块右下角位置的像素点离图片左边界的距离
        :param lower：区块右下角位置的像素点离图片上边界的距离
         故需满足：lower > upper、right > left
        :param save_path: 所截图片保存位置
        """
        img = Image.open(path)  # 打开图像
        box = (0, 86, 1080, 135)
        roi = img.crop(box)
        # 保存截取的图片
        roi.save(save_path)

    def is_sended_shop(self):
        fo = open(self.send_file, "a+")
        str = fo.read()
        
        if make_md5() in str:
            return True
        fo.write( make_md5()+"\n")
        return False

    def make_md5(self):
        image_cut_save(self.kefu, self.shop)

        file = open(save_path, "rb")
        md = hashlib.md5()
        md.update(file.read())
        res1 = md.hexdigest()
        file.close()
        return res1

    def iskefu(self):
        str1 = ImageToText('e:/%s' % self.screen)
        if( '推荐'.encode("utf-8") in str1.encode("utf-8") or '刷新'.encode("utf-8") in str1.encode("utf-8")):
            return True
        else:
            print(str1)
        return False

    #TagCircle('./view.png')
    def view(self, start_x,start_y,i):

        start_y = 300
        start_x = 250
        step = 300
        touch(start_x,start_y)
        print(start_x,start_y)
        time.sleep(6)
        self.back_index()
        time.sleep(3)
        
    #    
    def back_index(self):
        back()
                
    def is_view_page(self):
        screencap(screen_img)
        str = ImageToText('e:/%s' % self.screen)
        if( '我来说两句' in str):
            return True
        return False

    def is_main_page(self):
        screen_img = 'index.png'
    #    screencap(screen_img)
        str1 = ImageToText('e:/%s' % self.screen)
        if( '推荐'.encode("utf-8") in str1.encode("utf-8") or '刷新'.encode("utf-8") in str1.encode("utf-8")):
            return True
        else:
            print(str1)
        return False
    
   
    
import io
import sys
start_y = 2000
start_x = 300
ca = AutoReply()
i=1
print("start...")
while(True):
    end_y = start_y - 300
    print("234234234")
    swipe(start_x, start_y, start_x, end_y)
    time.sleep(3)
    ca.view(start_x,start_y,i)
    i=i+1


